// pages/home/home.js
var app = getApp();
const key = 'KTPBZ-XKYCK-ZNAJX-AZCIA-WNXQK-5DFEX' //地理位置的key值
Page({
  data: {
    currentTab: 0,
    isAdmin:false,
    statusBarHeight: 20,
    headerHeight: 40,
    areaName:'湖北省',
    windowHeight:800,
    showPlan:true,
    cityAreaArray: [],
    citysIndex: [0, 0],
    //这里只做tab名和显示图标
    items: [
      {
        "text": "市场",
        "iconPath": "/image/mall_off.png",
        "selectedIconPath": "/image/mall_on.png"
      },
      {
        "text": "购买方案",
        "iconPath": "/image/search_off.png",
        "selectedIconPath": "/image/search_on.png"
      },
      {
        "text": "我的论坛",
        "iconPath": "/image/luntan_off.png",
        "selectedIconPath": "/image/luntan_on.png"
      },
      {
        "text": "我的",
        "iconPath": "/image/mine_off.png",
        "selectedIconPath": "/image/mine_on.png"
      }
    ]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //如果是管理员
    // this.setData({
    //   isAdmin:true
    // })
    var headerHeight = 44;
    let calcHeight = ((headerHeight + app.globalData.systemInfo.statusBarHeight) * (750 / app.globalData.systemInfo.windowWidth));
    this.setData({
      statusBarHeight: app.globalData.systemInfo.statusBarHeight,
      headerHeight: headerHeight,
      windowHeight:app.globalData.windowHeight - 120 - calcHeight
    })
    this.shopComponent = this.selectComponent('#shopComponent');
    this.mineComponent = this.selectComponent('#mineComponent');
    this.adminComponent = this.selectComponent('#adminComponent');
    this.mallComponent = this.selectComponent('#mallComponent');
    this.luntanComponent = this.selectComponent('#luntanComponent');
    this.getAreaList();
    this.getLocation();
  },
  onShow: function () {
    //hgz/user/
    app.request.getReq('hgz/user').then(res =>{
      console.log(res);
      if(res.data.roleId == 1){
        app.globalData.isAdmin = true;
        this.setData({
          isAdmin : true
        })
      }else{
        app.globalData.isAdmin = false;
        this.setData({
          isAdmin : false
        })
      }
    })
  },
  //获取当前位置
  getLocation() {
    //开启定位
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        var longitude = res.longitude
        var latitude = res.latitude
        that.curCity(longitude, latitude)

      },
      fail(res) {
        that.getPromission();
      }
    })
  },
  //获取定位权限
  getPromission() {
    var that = this;
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: function (res) {
              if (res.cancel) {
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      wx.getLocation({
                        type: 'gcj02',
                        success(res) {
                          var longitude = res.longitude
                          var latitude = res.latitude
                          that.curCity(longitude, latitude)
                        }
                      })
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == true) {
          wx.getLocation({
            type: 'gcj02',
            success(res) {
              var longitude = res.longitude
              var latitude = res.latitude
              that.curCity(longitude, latitude)
            }
          })
        }
      }
    })
  },
  curCity: function (longitude, latitude) {
    var that = this;
    var newlatitude = latitude + ',' + longitude;
    wx.request({
      url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=',
      data: {
        location: newlatitude,
        key: key,
        output: "json"
      },
      success: function (res) {
        if (res.errMsg == 'request:ok') {
          var loc = res.data.result.ad_info;
          var area = loc.district;
          wx.setStorageSync("area", area);
          that.setData({
            areaName: area
          })
          that.shopComponent.setLoc(area);
          that.mallComponent.setLoc(area);

        }
      }
    })
  },
  getAreaList(){
    var that = this;
    app.request.postHeadForm('hgz/address/queryCityAndAreaList')
    .then(res => {
      that.setData({
        areaList: res.data
      })
      that.showAreaData2();
    })
  },
  showAreaData2(){
      var that = this;
      var citysIndex = that.data.citysIndex;
      var area = this.data.areaList;
      var areaList = [
        [],
        []
      ];
      for (var i = 0; i < area.length; i++) {
        areaList[0].push(area[i].cityName);
        if (i == citysIndex[0]) {
          for (var j = 0; j < area[i].areaVoList.length; j++) {
            areaList[1].push(area[i].areaVoList[j].areaName);
          }
        }
      }
      that.setData({
        cityAreaArray: areaList
      })
  },
  bindRegionColumnChange(e) {
    //滑动事件触发
    var that = this;
    var citysIndex = this.data.citysIndex;
    citysIndex[e.detail.column] = e.detail.value;
    that.setData({
      citysIndex: citysIndex
    })
    if (e.detail.column == 0) {
      that.showAreaData2();
    }
  },
  bindRegionChange(e) {
    //确认按钮触发
    var areabyte = e.detail.value;
    var areaList = this.data.cityAreaArray[1];
    var areaName = areaList[areabyte[1]];
    this.setData({
      areaName: areaName
    })
    var area = this.data.areaList;
    var areaId = "";
    for (var i = 0; i < area.length; i++) {
      for (var j = 0; j < area[i].areaVoList.length; j++) {
        if (area[i].areaVoList[j].areaName == areaName) {
          areaId = area[i].areaVoList[j].areaId;
          break;
        }
      }
    }
    if(this.data.currentTab == 0){
      this.mallComponent.showPicker(areaId,areaName);
    }
    // else if(this.data.currentTab == 1){
    //   this.shopComponent.showPicker(areaId,areaName);
    // }
  },
  swichNav: function (e) {
    let that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
        showPlan:false
      })

      if(e.target.dataset.current == 1){
        if(!this.data.isAdmin){
          // this.setData({
          //   showPlan:true
          // })
          this.shopComponent.onShow();
        }else{
          this.adminComponent.onShow();
        }
      }else if(e.target.dataset.current == 0){
        this.setData({
          showPlan:true
        })
      }else if(e.target.dataset.current == 3){
        this.mineComponent.onShow();
      }else if(e.target.dataset.current == 2){
        this.luntanComponent.onShow();
      }
    } 
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: "汉钢造",
      path: '/pages/home/home'
    }
  }
})